# -*- coding: utf-8 -*-
import scrapy
from dangdang.items import DangdangItem
from dangdang.util.mysql_DBUtils import mysql


class PythonSpider(scrapy.Spider):
    name = 'python'
    allowed_domains = ['search.dangdang.com']
    #start_urls = ['http://search.dangdang.com/']
    start_urls = ['http://search.dangdang.com/?key=python&act=input']


    def parse(self, response):
        allBooks = response.xpath('//div[@id="search_nature_rg"]/ul/li')
        for book in allBooks:
            item = DangdangItem()
            try:
                imgUrl = book.xpath('a/img/@src').extract()[0]
                bookname = book.xpath('p[@name="title"]/a/@title').extract()[0]
                price = book.xpath(
                'p[@class="price"]/span/text()').extract()[0]
                user = book.xpath(
                'p/span/a[@name="itemlist-author"]/@title').extract()[0]
                chubanshe = book.xpath('p/span/a[@name="P_cbs"]/@title').extract()[0]
                pinglunshu = book.xpath(
                'p/a[@class="search_comment_num"]/text()').extract()[0]
                jianjie = book.xpath('p[@class="detail"]/text()').extract()[0]
            except IndexError:
                pass
            item['imgUrl'] = imgUrl
            item['name'] = bookname
            item['price'] = price
            item['user'] = user
            item['chubanshe'] = chubanshe
            item['pinglunshu'] = pinglunshu
            item['jianjie'] = jianjie

            #print(item)
            yield item
        #mysql.end("commit")
            db_write(item)
        try:
            next_url = response.xpath('//div[@class="paging"]/ul/li[@class="next"]/a/@href').extract()[0]
            top_url = 'http://search.dangdang.com'
            next_url = top_url + next_url
            print('下一页地址：'+next_url)
            yield self.make_requests_from_url(next_url)
        except IndexError:
            pass
    mysql.dispose()


def db_write(param):
    #print(param)
    try :
        sql = "insert into book (imgUrl,bookname,price,user,chubanshe,pinglunshu,jianjie) "
        sql = sql + "VALUES(%(imgUrl)s,%(name)s, %(price)s,%(user)s,%(chubanshe)s,%(pinglunshu)s,%(jianjie)s)"
        mysql.insert(sql, param)
    except Exception as e:
        print(e)
